package com.xpec.net.xnetty;

import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;

import com.xpec.net.NetFuture;
import com.xpec.net.NetFutureListener;

/**
 * 僅提供 XNet 內部使用, 外部使用抽象層 NetFutureListener
 * @author linyuntai
 */
public class XNetFutureListener implements ChannelFutureListener {

	private NetFuture future;
	private NetFutureListener listener;
	public XNetFutureListener(NetFuture future, NetFutureListener listener){
		this.future = future;
		this.listener = listener;
	}

	@Override
	public void operationComplete(ChannelFuture channelfuture) throws Exception
	{
		if(listener!=null){
			listener.operationComplete(future);
		}
	}

//	@Override
//	public void operationProgressed(ChannelFuture channelfuture, long l,
//			long l1, long l2) throws Exception {
//		
//	}
	
	/**
	 * 幫忙把外部自己實現的 NetFutureListener add 進真正的 Netty ChannelFuture
	 * @param listener 外部實現的 listener
	 * @param future Netty 的 IO Operation Netty ChannelFuture
	 */
	public static XNetFuture bindTheUserListener(NetFutureListener listener, ChannelFuture channelFuture){
		if(listener==null || channelFuture==null) return null;
		
		XNetFuture future = new XNetFuture( channelFuture );
		XNetFutureListener xNetListener = new XNetFutureListener(future, listener);
		channelFuture.addListener( xNetListener );
		
		return future;
	}

}
